//
//  StrDetailTableViewController.m
//  TeamProject_Travel
//
//  Created by lanou3g on 16/4/5.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "StrDetailTableViewController.h"
#import "StrDetailTableViewCell.h"
#import "UIImageView+WebCache.h"
@interface StrDetailTableViewController ()

@property(nonatomic,strong)NSMutableArray * dataArray;
@property(nonatomic,assign)CGFloat widthOFString;
@property(nonatomic,assign)BOOL isDisplay;

@end

@implementation StrDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isDisplay = YES;
    [self.tableView registerClass:[StrDetailTableViewCell class] forCellReuseIdentifier:@"HaLiLuYa"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.allDataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.dataArray = [NSMutableArray array];
    NSArray *array = self.allDataArray[section][@"sections"];
    [self.dataArray addObjectsFromArray:array];
    if (self.isDisplay == YES) {
        return self.dataArray.count;
    }else{
        return 0;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StrDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HaLiLuYa" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[StrDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HaLiLuYa"];
    }
    
    NSArray *array = self.allDataArray[indexPath.section][@"sections"];
    
    if (indexPath.row > 0) {
        cell.titlelabel.text = array[indexPath.row][@"title"];
        cell.descriptionlabel.text = array[indexPath.row][@"description"];
        cell.descriptionlabel.frame = CGRectMake(10, CGRectGetMaxY(cell.titlelabel.frame), [UIScreen mainScreen].bounds.size.width - 20, [self getTrueHeightWithDescription:array[indexPath.row][@"description"]]);
        
        NSArray *photoArray = array[indexPath.row][@"photos"];
        if (photoArray.count != 0) {
            CGFloat contentWidth = 0;
            for (int i = 0; i < photoArray.count; i++) {
                CGFloat width = [photoArray[i][@"image_width"] floatValue] *150 / [photoArray[i][@"image_height"] floatValue];
                contentWidth += width;
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(contentWidth - width, 0, width, 150)];
                imageView.layer.cornerRadius = 5;
                imageView.layer.borderWidth = 1;
                imageView.layer.masksToBounds = YES;
                imageView.layer.borderColor = [UIColor whiteColor].CGColor;
                [imageView sd_setImageWithURL:[NSURL URLWithString:photoArray[i][@"image_url"]]];
                [cell.imageScrollView addSubview:imageView];
                
                if (contentWidth <= [UIScreen mainScreen].bounds.size.width) {
                    cell.imageScrollView.frame = CGRectMake(5, CGRectGetMaxY(cell.descriptionlabel.frame) + 20, contentWidth, 150);
                }else{
                    cell.imageScrollView.frame = CGRectMake(5, CGRectGetMaxY(cell.descriptionlabel.frame) + 20, [UIScreen mainScreen].bounds.size.width - 10, 150);
                }
                
                cell.imageScrollView.contentSize = CGSizeMake(contentWidth, 150);
            }
            self.widthOFString = [self getTrueHeightWithDescription:array[indexPath.row][@"description"]] + 230;
            if (contentWidth > [UIScreen mainScreen].bounds.size.width) {
                cell.imageScrollView.pagingEnabled = YES;
            }
        }else{
            cell.imageScrollView.frame = CGRectMake(5, CGRectGetMaxY(cell.descriptionlabel.frame), 0, 0);
            self.widthOFString = self.widthOFString = [self getTrueHeightWithDescription:array[indexPath.row][@"description"]] + 80;
        }
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.widthOFString;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(5, 0, [UIScreen mainScreen].bounds.size.width, 60)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *labelA = [[UILabel alloc]initWithFrame:CGRectMake(10, 15, 5, 30)];
    labelA.backgroundColor = [UIColor purpleColor];
    [view addSubview:labelA];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 5, 200, 50)];
    titleLabel.text = self.allDataArray[section][@"title"];
    titleLabel.font = [UIFont systemFontOfSize:20];
    [view addSubview:titleLabel];
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 40, 15, 30, 30)];
    [button addTarget:self action:@selector(displayCellWithButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:[UIImage imageNamed:@"shouqi.png"] forState:UIControlStateNormal];
    button.tag = section;
    [view addSubview:button];
    
    return view;
}

- (void)displayCellWithButton:(UIButton *)sender{
//    if (self.isDisplay == YES) {
//       [sender setImage:[UIImage imageNamed:@"zhankai.png"] forState:UIControlStateNormal];
//        self.isDisplay = NO;
//    
//    }else{
//        [sender setImage:[UIImage imageNamed:@"shouqi.png"] forState:UIControlStateNormal];
//        self.isDisplay = YES;
//    }
//    [self tableView:self.tableView numberOfRowsInSection:sender.tag];
//    [self tableView:self.tableView heightForHeaderInSection:60];
//    [self.tableView reloadData];
}

- (CGFloat)getTrueHeightWithDescription:(NSString *)discuss
{
    CGRect rect = [discuss boundingRectWithSize:(CGSizeMake([UIScreen mainScreen].bounds.size.width, 200000)) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    return rect.size.height;
}

@end
